#!/usr/bin/env bash
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# Function :CentOS7.X 在线更换yum源
# Platform :RedHatEL7.x Based Platform
# Version  :1.2
# Date     :2021-07-06
# Author   :mugoLH
# Contact  :houxiaoshuai@baidu.com & hxsaj@126.com
# Company  :
# depend on:init_os.sh
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
BAK_DATE=$(date +%Y-%m-%d_%H%M%S)
BAK_DIR=/etc/yum.repos.d/${BAK_DATE}

#  备份源文件
function backup_yum_source(){
	if [ $(ls /etc/yum.repos.d/ | grep -v epel | grep "repo$" | wc -l) -gt 0 ];then
		mkdir -p ${BAK_DIR}
		for i in $(ls /etc/yum.repos.d/ | grep -v epel | grep "repo$");do
			mv /etc/yum.repos.d/${i} ${BAK_DIR}/
		done
	fi
}

function source_aliyun(){
	echo '
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
enabled=1
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
enabled=1
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
enabled=1
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
enabled=1
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=0
enabled=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

' >/etc/yum.repos.d/Centos-aliyun-7.repo
}

function source_163(){
	echo '
[base]
name=CentOS-$releasever - Base - 163.com
baseurl=http://mirrors.163.com/centos-vault/7.9.2009/os/x86_64/
enabled=1
gpgcheck=0
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates - 163.com
baseurl=http://mirrors.163.com/centos-vault/7.9.2009/updates/x86_64/
enabled=1
gpgcheck=0
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - 163.com
baseurl=http://mirrors.163.com/centos-vault/7.9.2009/extras/x86_64/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - 163.com
baseurl=http://mirrors.163.com/centos-vault/7.9.2009/centosplus/x86_64/
gpgcheck=0
enabled=1
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

' >/etc/yum.repos.d/Centos-163-7.repo
}

function source_ustc(){
	echo  '
[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.ustc.edu.cn/centos-vault/7.9.2009/os/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.ustc.edu.cn/centos-vault/7.9.2009/updates/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.ustc.edu.cn/centos-vault/7.9.2009/extras/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.ustc.edu.cn/centos-vault/7.9.2009/centosplus/x86_64/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

' >/etc/yum.repos.d/Centos-ustc-7.repo
}

function source_sohu(){
	echo '
[base]
name=CentOS-$releasever - Base
baseurl=http://mirrors.sohu.com/centos/$releasever/os/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=http://mirrors.sohu.com/centos/$releasever/updates/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=http://mirrors.sohu.com/centos/$releasever/extras/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=http://mirrors.sohu.com/centos/$releasever/centosplus/$basearch/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

'>/etc/yum.repos.d/Centos-sohu-7.repo
}

function source_shoudu(){
	echo '
[base]
name=CentOS-$releasever - Base
baseurl=http://mirrors.yun-idc.com/centos-vault/7.9.2009/os/x86_64/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=http://mirrors.yun-idc.com/centos-vault/7.9.2009/updates/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=http://mirrors.yun-idc.com/centos-vault/7.9.2009/extras/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=http://mirrors.yun-idc.com/centos-vault/7.9.2009/centosplus/$basearch/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

' >/etc/yum.repos.d/Centos-shoudu-7.repo
}

#  1，备份源文件
backup_yum_source
#  2，部署新的源文件
if [[ $# > 0 ]];then
	case ${1} in
		"ali"   ) source_aliyun   ;;
		"163"   ) source_163      ;;
		"ustc"  ) source_ustc     ;;
		"sohu"  ) source_sohu     ;;
		"shoudu") source_shoudu   ;;
	esac
else
	# ---------------- 打印服务器列表 -------------------------------#
	# -------------------------------------------------------------#
	echo "
	1，阿里源
	2，网易源
	3，中科大源
	4，搜狐源
	5，首都在线源
	"
	# 选择源
	read -t 30 -p  "选择源编号：" Choices_Source
	case ${Choices_Source} in
		# 阿里源
		"1" ) source_aliyun   ;;
		# 网易源
		"2" ) source_163      ;;
		# 中科大源
		"3" ) source_ustc     ;;
		# 搜狐源
		"4" ) source_sohu     ;;
		# 首都在线源
		"5" ) source_shoudu   ;;
		# 输入不规范提示并退出
		* ) echo "输入有误，请重新执行！" &&exit                   ;;
    esac
fi
#  3，更新源库
yum clean all && yum makecache fast